package com.service.business.member.controller;

import com.service.business.base.dto.LoginUserSession;
import com.service.business.base.service.VerifyCodeService;
import com.service.business.framework.log.RequestLog;
import com.service.business.member.controller.rep.*;
import com.service.business.member.controller.req.*;
import com.service.business.member.service.MemberAssetService;
import com.service.business.member.service.MemberService;
import com.service.business.member.service.MemberVerifiedService;
import com.service.business.support.SessionUtil;
import com.service.business.utils.CheckIdCardUtil;
import com.service.business.utils.IpAddressUtil;
import com.service.core.exception.CommonException;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.MultiResponse;
import com.service.core.rest.response.OneResponse;
import com.service.core.rest.response.PageResponse;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.enums.MemberVerifyEnums;
import com.service.dao.model.Member;
import com.service.dao.model.MemberVerified;
import com.service.utility.common.UUIDUtil;
import com.service.utility.modelmapper.ModelMapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年09月20日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@RestController()
@RequestMapping("member")
@Api(tags = "会员相关信息")
public class MemberController extends BaseController {

    @Autowired
    private SessionUtil sessionUtil;
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberAssetService memberAssetService;

    @Autowired
    private VerifyCodeService verifyCodeService;

    @Autowired
    private MemberVerifiedService memberVerifiedService;

    @ApiOperation("获取会员资产总计")
    @RequestLog("获取会员资产总计")
    @PostMapping("asset")
    public OneResponse<MemberAssetRep> asset() {
        return successQueryOneResponse(memberAssetService.memberAsset());
    }


    @ApiOperation("修改登录密码")
    @RequestLog("修改登录密码")
    @PostMapping("modify-pwd/login")
    public BaseResponse modifyLoginPwd(@ApiParam @RequestBody @Validated ModifyPwdReq modifyPwdReq) {
        memberService.modifyMemberLoginPwd(modifyPwdReq);
        return successUpdateBaseResponse();
    }

    @ApiOperation("修改支付密码")
    @RequestLog("修改支付密码")
    @PostMapping("modify-pwd/pay")
    public BaseResponse modifySafetyPwd(@ApiParam @RequestBody @Validated ModifyPwdReq modifyPwdReq) {
        memberService.modifyMemberSafetyPwd(modifyPwdReq);
        return successUpdateBaseResponse();
    }


    @ApiOperation("查询2FA状态")
    @RequestLog("查询2FA绑定状态")
    @GetMapping("fa/status")
    public BaseResponse faStatus() { return successQueryOneResponse(memberService.faStatus());}

    @ApiOperation("获取2FA私钥")
    @RequestLog("生成2FA私钥")
    @GetMapping("fa/get")
    public BaseResponse getTwoFa(){
        return successQueryOneResponse(memberService.getTwoFa());
    }


    @ApiOperation("绑定2FA验证码")
    @RequestLog("绑定2FA验证码")
    @PostMapping("fa/bind")
    public BaseResponse bindTwoFa(@ApiParam @RequestBody @Validated TwoFaReq twoFaReq){
        memberService.bindTwoFa(twoFaReq);
        return successBaseResponse();
    }

    @ApiOperation("解绑2FA验证码")
    @RequestLog("解绑2FA验证码")
    @PostMapping("fa/unbind")
    public BaseResponse unbindTwoFa(@ApiParam @RequestBody @Validated TwoFaReq twoFaReq){
        memberService.unbindTwoFa(twoFaReq);
        return successBaseResponse();
    }


    @ApiOperation("校验2FA验证码")
    @RequestLog("用户校验2FA")
    @PostMapping("fa/check")
    public BaseResponse checkTwoFa(@ApiParam @RequestBody @Validated TwoFaReq twoFaReq){
        boolean checkRst = memberService.checkTwoFa(twoFaReq);
        return successBaseResponse(checkRst ? "Success" : "Wrong");
    }

    @ApiOperation("会员详情")
    @RequestLog("会员详情")
    @GetMapping("detail")
    public OneResponse<MemberDetailRep> detail() {
        LoginUserSession session = sessionUtil.getUser();
        Member member = memberService.getById(session.getUserId());
        if(!ObjectUtils.isEmpty(member)){
            MemberVerified memberVerified = memberVerifiedService.getById(member.getId());
            if(!ObjectUtils.isEmpty(memberVerified)){
                member.setVerifyStatus(memberVerified.getVerifyStatus());
            }else{
                member.setVerifyStatus(0);
            }
        }
        return successQueryOneResponse(ModelMapperUtil.map(member, MemberDetailRep.class));
    }

    @RequestLog("修改会员信息")
    @ApiOperation("修改会员信息")
    @PostMapping("update")
    public BaseResponse updateMemberInfo(@RequestBody @Validated MemberUpdateReq updateReq) {
        memberService.updateMemberInfo(updateReq);
        return successUpdateBaseResponse();
    }

    @RequestLog("根据token获取会员信息")
    @ApiOperation("根据token获取会员信息")
    @GetMapping("info")
    public OneResponse<MemberInfoRep> getMemberByToken(){
        return successQueryOneResponse(memberService.getMemberInfoByToken());
    }
    @ApiOperation("会员团队统计")
    @PostMapping("team/total")
    public OneResponse<TotalMemberTeamRep> totalMemberTeam() {
        return successQueryOneResponse(memberService.totalMemberTeam());
    }

    @ApiOperation("会员团队")
    @PostMapping("team/page")
    public PageResponse<MemberTeamPageRep> pageMemberTeam(@RequestBody MemberTeamPageReq pageReq) {
        return successQueryPageResponse(memberService.pageMemberTeam(pageReq));
    }

    @ApiOperation("根据账号查询会员")
    @PostMapping("list")
    public MultiResponse<MemberInfoRep> listMemberByUserAccount(@RequestBody() MemberQueryReq userAccount){
        return successQueryMultiResponse(memberService.listByUserAccount(userAccount.getUserAccount()));
    }

    @ApiOperation("用户申请实名认证")
    @PostMapping("apply-verify")
    public BaseResponse applyVerified(@RequestBody MemberVerifiedApplyReq req){
        LoginUserSession session = sessionUtil.getUser();
        MemberVerified memberVerified = new MemberVerified();
        memberVerified.setMemberId(session.getUserId());
        if(!CheckIdCardUtil.validate(req.getIdCardNo())){
            throw new CommonException("身份证号码有误","args_verify_idCardNo_check");
        }
        memberVerified.setReceiptAddress(req.getReceiptAddress());
        memberVerified.setPhone(req.getPhone());
        memberVerified.setIdCardNo(req.getIdCardNo());
        memberVerified.setPhotoFront(req.getPhotoFront());
        memberVerified.setPhotoBack(req.getPhotoBack());
        memberVerified.setRealName(req.getRealName());
        memberVerified.setVerifyStatus(MemberVerifyEnums.CHECKING.getCode());
        memberVerifiedService.saveOrUpdate(memberVerified);
        return successSaveBaseResponse();
    }
}
